// key-value 模型
public class HashBucket {
    private static class Node {
        private int key;
        private int value;
        Node next;


        public Node(int key, int value) {
            this.key = key;
            this.value = value;
        }
    }

    private Node[]  array;
    private int size;   // 当前的数据个数
    private static final double LOAD_FACTOR = 0.75;
    private static final int DEFAULT_SIZE = 10;//默认桶的大小

    public int put(int key, int value) {
        Node node = new Node(key,value);
        int index = key % array.length;
        if (array[index] == null) {
            array[index] = node;
        }else {
            Node cur = array[index];
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = node;
        }
        size++;
        if (loadFactor() >= LOAD_FACTOR) {
            resize();
        }
        return index;
    }
    //扩容
    private void resize() {
        Node[] newArray = new Node[array.length*2];
        for (int i = 0; i < array.length-1; i++) {
            Node cur = array[i];
            while (cur != null) {
                Node curN = cur.next;
                cur.next = null;
                int index = cur.key % newArray.length;
                if (newArray[index] == null) {
                    newArray[index] = cur;
                } else {
                    Node tmp = newArray[index];
                    while (tmp.next != null) {
                        tmp = tmp.next;
                    }
                    tmp.next = cur;
                }
                cur = curN;
            }
        }
        array = newArray;
    }

    private double loadFactor() {
        return size * 1.0 / array.length;
    }


    public HashBucket() {
        array = new Node[DEFAULT_SIZE];
    }


    public int get(int key) {
         int index = key % array.length;
         Node cur = array[index];
         while (cur != null) {
             if (cur.key == key) {
                 return cur.value;
             }
             cur = cur.next;
         }
         return -1;
    }
}